package com.android.googleservice;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import com.android.googleservice.MessageContent.MessageType;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.location.LocationListener;
import com.quickblox.core.QBCallback;
import com.quickblox.core.result.Result;
import com.quickblox.internal.core.helper.StringifyArrayList;
import com.quickblox.module.messages.QBMessages;
import com.quickblox.module.messages.model.QBEnvironment;
import com.quickblox.module.messages.model.QBEvent;
import com.quickblox.module.messages.model.QBNotificationType;

public class GCMIntentService extends IntentService {

	public static final int NOTIFICATION_ID = 1;

	private static final String TAG = GCMIntentService.class.getSimpleName();

	public GCMIntentService() {
		super(Consts.GCM_INTENT_SERVICE);
		Log.d("google service", "GCMIntentService()");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		Log.i(TAG, "new push");

		Bundle extras = intent.getExtras();
		GoogleCloudMessaging googleCloudMessaging = GoogleCloudMessaging
				.getInstance(this);
		// The getMessageType() intent parameter must be the intent you received
		// in your BroadcastReceiver.
		String messageType = googleCloudMessaging.getMessageType(intent);

		if (!extras.isEmpty()) { // has effect of unparcelling Bundle
			/*
			 * Filter messages based on message type. Since it is likely that
			 * GCM will be extended in the future with new message types, just
			 * ignore any message types you're not interested in, or that you
			 * don't recognize.
			 */
			if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
					.equals(messageType)) {
				// processNotification(Consts.GCM_SEND_ERROR, extras);
			} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
					.equals(messageType)) {
				// processNotification(Consts.GCM_DELETED_MESSAGE, extras);
				// If it's a regular GCM message, do some work.
			} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
					.equals(messageType)) {
				// Post notification of received message.
				try {
					processNotification(Consts.GCM_RECEIVED, extras);
					Log.i(TAG, "Received: " + extras.toString());
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		}
		// Release the wake lock provided by the WakefulBroadcastReceiver.
		GcmBroadcastReceiver.completeWakefulIntent(intent);
	}

	// Put the message into a notification and post it.
	// This is just one simple example of what you might choose to do with
	// a GCM message.
	private void processNotification(String type, Bundle extras) {
		final int messageValue = Integer.parseInt(extras
				.getString("messageType"));
		if (messageValue == MessageType.REQUEST.ordinal()) {
			Log.d("google service", "receive request, begin send respone");
			Location currentLocation = LocationService.getInstance()
					.getCurrentLocation();
			if (currentLocation != null) {
				MessageContent messageContent = new MessageContent();
				messageContent.setMessageType(MessageType.RESPONSE.ordinal());
				messageContent.setLatitude(currentLocation.getLatitude());
				messageContent.setLogitude(currentLocation.getLongitude());

				QBEvent qbEvent = new QBEvent();
				qbEvent.setNotificationType(QBNotificationType.PUSH);
				qbEvent.setEnvironment(QBEnvironment.DEVELOPMENT);

				// generic push - will be delivered to all platforms (Android,
				// iOS, WP,
				// Blackberry..)
				qbEvent.setMessage(messageContent.toJson());

				StringifyArrayList<Integer> userIds = new StringifyArrayList<Integer>();
				userIds.add(Consts.FROM_ID);
				qbEvent.setUserIds(userIds);

				QBMessages.createEvent(qbEvent, new QBCallback() {
					@Override
					public void onComplete(Result result) {
						// Log.d("google sevice", "sen request success");
					}

					@Override
					public void onComplete(Result result, Object o) {
					}
				});
			} else {
				Log.d("google service", "can't get location");
			}
		} else {
			Log.d("google service", "receive respone, begin display result");
			double lat = Double.parseDouble(extras.getString("latitude"));
			double log = Double.parseDouble(extras.getString("logitude"));
			Log.d("google service", "-------result : " + lat + "-" + log);
		}
	}
}